package com.zpl.order.controller;


import com.zpl.core.Result;
import com.zpl.order.entity.Goods;
import com.zpl.order.entity.OrderDetails;
import com.zpl.order.entity.vo.OrderDetailsImageVO;
import com.zpl.order.feign.GoodsFeign;
import com.zpl.order.service.OrderDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/orderDetails")
@Api(value = "OrderDetailsController", tags = {"订单细节管理"})
public class OrderDetailsController {

    @Resource
    private OrderDetailsService orderDetailsService;

    @Resource
    private GoodsFeign goodsFeign;

    @ApiOperation(value = "1、删除订单细节信息", notes = "删除订单细节信息", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "id", dataType = "int",required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        if (orderDetailsService.delete(id)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @ApiOperation(value = "2、修改订单细节信息", notes = "修改订单细节信息", httpMethod = "PUT")
    @ApiImplicitParam(name = "orderDetails", value = "订单细节信息", dataType = "OrderDetails",required = true)
    @PutMapping
    public Result update(@RequestBody OrderDetails orderDetails) {
        if (orderDetailsService.update(orderDetails)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @ApiOperation(value = "3、获取订单细节信息", notes = "按id获取订单细节信息", httpMethod = "GET")
//    @ApiImplicitParam(name = "orderDetails", value = "订单细节信息", dataType = "OrderDetails",required = true)
    @GetMapping("/list/{orderId}")
    public Result getOrderDetailsByOrderId(@PathVariable("orderId") Long orderId) {
        List<OrderDetails> list = orderDetailsService.selectByOrderId(orderId);
        List<OrderDetailsImageVO> list1 = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            OrderDetailsImageVO orderDetailsImageVO = new OrderDetailsImageVO();
            orderDetailsImageVO.setUpdateDate(list.get(i).getUpdateDate());
            orderDetailsImageVO.setOperatorId(list.get(i).getOperatorId());
            orderDetailsImageVO.setId(list.get(i).getId());
            orderDetailsImageVO.setCreateDate(list.get(i).getCreateDate());
            orderDetailsImageVO.setWId(list.get(i).getWId());
            orderDetailsImageVO.setProductPrice(list.get(i).getProductPrice());
            orderDetailsImageVO.setProductName(list.get(i).getProductName());
            orderDetailsImageVO.setProductId(list.get(i).getProductId());
            orderDetailsImageVO.setProductCnt(list.get(i).getProductCnt());
            orderDetailsImageVO.setOrderId(list.get(i).getOrderId());
            orderDetailsImageVO.setFeeMoney(list.get(i).getFeeMoney());
            orderDetailsImageVO.setAverageCost(list.get(i).getAverageCost());
            Goods goods = goodsFeign.getGoodFeign(list.get(i).getProductId());
            orderDetailsImageVO.setMainImage(goods.getMainImage());
            list1.add(orderDetailsImageVO);
        }
        return Result.success(list1);
    }

}
